<?php
namespace core;


/**
 * 确认目录存在
 * @param string $path
 */
function path_sure($path) {
    if (!file_exists($path)) mkdir($path, 0777, true);
}

/**
 * 获取原始 uri
 * @example /admin/Index/Index.html?a=111&b=222 => /admin/Index/Index.html
 * @param string $uri
 * @return string
 */
function url_original($uri) {
    $t = explode('?', $uri, 2);

    return $t[0];
}

/**
 * 通过 uri 得到 mod
 * @example /admin/Index/Index.html => [admin, Index, Index]
 * @param string $uri
 * @param string $root
 * @return array
 */
function url_to_mod($uri, $root = '/') {
    $uri 	= explode($root, $uri, 2);
    $uri 	= explode('.', urldecode($uri[1]), 2);
    $uri	= explode('/', $uri[0]);

    $mod	= [];
    foreach ($uri as $v) $v !== '' && $mod[] = $v;

    return $mod;
}

/**
 * 获取完整 uri
 * @example http://xxx.com|https://xxx.com|http://xxx.com:88|https://xxx.com:88
 * @return string
 */
function uri_full() {
    $pageURL = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    $pageURL .= $_SERVER['HTTP_HOST'];

    return $pageURL;
}
